package com.augurit.common.workdayholiday.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.augurit.common.entity.WhSpecialTime;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.workdayholiday.web.form.WhSpecialTimeForm;

public class WhSpecialTimeConvertor {
	
	public static WhSpecialTimeForm convertVoToForm(WhSpecialTime entity) {
		if(entity != null) {
			WhSpecialTimeForm form = new WhSpecialTimeForm();
			form.setStartHour(entity.getStartHour());
			form.setId(entity.getId());
			form.setStartMinute(entity.getStartMinute());
			form.setEndHour(entity.getEndHour());
			form.setEndMinute(entity.getEndMinute());
			form.setIsHoliday(entity.getIsHoliday());
			form.setDay(DateUtils.dateTimeToString(entity.getDay(),"yyyy-MM-dd"));
			form.setStartTime(entity.getStartTime());
			form.setEndTime(entity.getEndTime());
			form.setMemo(entity.getMemo());
			form.setIsAllDay(entity.getIsAllDay());
			
			form.setHolidayOrWork(entity.getIsHoliday().equals("Y")?WhSpecialTimeForm.HOLIDAY_STATE:WhSpecialTimeForm.WORK_STATE);
			form.setTimeSpanDesc(entity.getIsAllDay().equals("Y")?WhSpecialTimeForm.ALL_DAY_TIME_SPAN:"");
			return form;
		}else
			return null;
	}
	
	public static boolean isAllDay(WhSpecialTime entity){
		return (entity.getStartHour() == null && entity.getStartMinute() == null 
				&& entity.getEndHour() == null && entity.getEndMinute() == null);
	}
	
	public static void convertFormToVo(WhSpecialTimeForm form, WhSpecialTime entity){
		try{
			if(entity != null && form != null) {
				entity.setStartHour(form.getStartHour());
				entity.setId(form.getId());
				entity.setStartMinute(form.getStartMinute());
				entity.setEndHour(form.getEndHour());
				entity.setEndMinute(form.getEndMinute());
				entity.setIsAllDay(form.getIsAllDay());
				entity.setStartTime(form.getIsAllDay().equals("Y")?"":form.getStartTime());
				entity.setEndTime(form.getIsAllDay().equals("Y")?"":form.getEndTime());
				if(form.getIsHoliday() != null && form.getIsHoliday().trim().length() > 0)
					entity.setIsHoliday(form.getIsHoliday().trim());
				entity.setDay(DateUtils.stringToDate(form.getDay(), "yyyy-MM-dd"));
				if(form.getMemo() != null && form.getMemo().trim().length() > 0)
					entity.setMemo(form.getMemo().trim());
			}
		}
		catch(Exception e){
			
		}
		
	}
	
	public static List<WhSpecialTimeForm> convertVoListToFormList(List<WhSpecialTime> whSpecialTimeList) {
		if(whSpecialTimeList != null && whSpecialTimeList.size() > 0) {
			List<WhSpecialTimeForm> whSpecialTimeFormList = new ArrayList();
			for(int i=0; i<whSpecialTimeList.size(); i++) {
				whSpecialTimeFormList.add(convertVoToForm(whSpecialTimeList.get(i)));
			}
			return whSpecialTimeFormList;
		}
		return null;
	}
	
	public static List<Map> convertVoListToMapList(List<WhSpecialTime> whSpecialTimeList) {
		if(whSpecialTimeList != null && whSpecialTimeList.size() > 0) {
			List<Map> mapList = new ArrayList();
			for(int i=0; i<whSpecialTimeList.size(); i++) {
				WhSpecialTime entity = whSpecialTimeList.get(i);
				Map<String, String> map = new HashMap();

				map.put("startHour", entity.getStartHour() == null ? "" : entity.getStartHour().toString());
				map.put("id", entity.getId() == null ? "" : entity.getId().toString());
				map.put("startMinute", entity.getStartMinute() == null ? "" : entity.getStartMinute().toString());
				map.put("endHour", entity.getEndHour() == null ? "" : entity.getEndHour().toString());
				map.put("endMinute", entity.getEndMinute() == null ? "" : entity.getEndMinute().toString());
				map.put("isHoliday", entity.getIsHoliday());
				map.put("day", DateUtils.dateTimeToString(entity.getDay(), "yyyy-MM-dd"));
				map.put("memo", entity.getMemo());
				
				mapList.add(map);
			}
			return mapList;
		}
		return null;
	}
	
	public static List<WhSpecialTime> convertFormListToVoList(List<WhSpecialTimeForm> whSpecialTimeFormList) {
		if(whSpecialTimeFormList != null && whSpecialTimeFormList.size() > 0) {
			List<WhSpecialTime> whSpecialTimeList = new ArrayList();
			for(int i=0; i<whSpecialTimeFormList.size(); i++) {
				WhSpecialTime whSpecialTime = new WhSpecialTime();
				convertFormToVo(whSpecialTimeFormList.get(i), whSpecialTime);
				whSpecialTimeList.add(whSpecialTime);
			}
			return whSpecialTimeList;
		}
		return null;
	}
}